package com.lute.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.springframework.security.oauth2.common.json.JSONException;
import org.springframework.security.oauth2.common.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lute.model.Admin;
import com.lute.model.Approver;
import com.lute.model.BusinessUnit;
import com.lute.model.Client;
import com.lute.model.ClientAccountingPeriod;
import com.lute.model.ClientAdmin;
import com.lute.model.ClientAdminProfile;
import com.lute.model.ClientCategory;
import com.lute.model.ClientProfile;
import com.lute.model.CompanyCategory;
import com.lute.model.Employee;
import com.lute.model.EmployeeAccountingPeriod;
import com.lute.model.EmployeeAccountingPeriodState;
import com.lute.model.EmployeeEntry;
import com.lute.model.EmployeeProfile;
import com.lute.model.NotepadEntry;
import com.lute.model.Position;
import com.lute.model.Role;
import com.lute.model.User;
import com.lute.utils.ServerErrorResponse;

@Controller
public class ResetDataBaseServlet {
	
	@RequestMapping (value="/resetDatabase",method= RequestMethod.GET)
	public @ResponseBody String resetDatabase(HttpServletRequest request) throws JSONException {
		String result = "";
		JSONObject jsonReq;
		JSONObject jsonRes = new JSONObject();
		
		HttpSession session = request.getSession(false);
		if(session == null) {
			jsonRes.put("result", ServerErrorResponse.USER_NOT_LOGGED_IN.toString());
			result = jsonRes.toString();
			return result;
		}
		
		String role = (String)session.getAttribute("role");
		
		/* check  authorization */
		if(!(role.equals("admin"))) {
			jsonRes.put("result", ServerErrorResponse.USER_NOT_ENTITLED.toString());
			result = jsonRes.toString();
			System.out.println(result);
			return result;
		}
		
		AnnotationConfiguration config = new AnnotationConfiguration();
		config.addAnnotatedClass(Admin.class);
		config.addAnnotatedClass(Approver.class);
		config.addAnnotatedClass(BusinessUnit.class);
		config.addAnnotatedClass(Client.class);
		config.addAnnotatedClass(ClientAccountingPeriod.class);
		config.addAnnotatedClass(ClientAdmin.class);
		config.addAnnotatedClass(ClientAdminProfile.class);
		config.addAnnotatedClass(ClientCategory.class);
		config.addAnnotatedClass(ClientProfile.class);
		config.addAnnotatedClass(CompanyCategory.class);
		config.addAnnotatedClass(Employee.class);
		config.addAnnotatedClass(EmployeeAccountingPeriod.class);
		config.addAnnotatedClass(EmployeeAccountingPeriodState.class);
		config.addAnnotatedClass(EmployeeEntry.class);
		config.addAnnotatedClass(EmployeeProfile.class);
		config.addAnnotatedClass(NotepadEntry.class);
		config.addAnnotatedClass(Position.class);
		config.addAnnotatedClass(Role.class);
		config.addAnnotatedClass(User.class);
		

		config.configure("hibernate.cfg.xml");
		try {
			new SchemaExport(config).drop(true, true);
		}catch(Exception e) {
			jsonRes.put("result", ServerErrorResponse.AUTHENTICATION_SERVICE_DOWN.toString());
			result = jsonRes.toString();
			System.out.println(result);
			return result;
		}
		
		jsonRes.put("result","OK");
		result = jsonRes.toString();
		System.out.println(result);
		return result;
	}
}
